﻿using UnityEngine;
using System.Collections;

public class InGameMenu : MonoBehaviour {
	
	public UISprite musicStatus;
	public UISprite soundStatus;

	bool isDisconnectedWithPlayer = false;
	public GameObject connectionTerminated;

	void Start()
	{
		musicStatus.enabled =(DataScript.IsGameState(GAMESTATE.HAS_MUSIC_ON) != 0)? true: false;
		soundStatus.enabled = (DataScript.IsGameState(GAMESTATE.HAS_SOUND_EFFECTS_ON) != 0)? true: false;
	}
	public void ShowMenu()
	{
		UIPlayTween tween = UIButton.current.GetComponent<UIPlayTween>();
		
		if(tween.playDirection == AnimationOrTween.Direction.Forward)
		{
			tween.playDirection = AnimationOrTween.Direction.Reverse;
		}
		else if(tween.playDirection == AnimationOrTween.Direction.Reverse)
		{
			tween.playDirection = AnimationOrTween.Direction.Forward;
		}
	}
	
	public void SoundOnCLick()
	{
		if(DataScript.IsGameState(GAMESTATE.HAS_SOUND_EFFECTS_ON) != 0)
		{
			Debug.Log("Turn of Sound Effect");
			DataScript.TurnOffGameState(GAMESTATE.HAS_SOUND_EFFECTS_ON);
			//NGUITools.SetActive(soundStatus.gameObject, false);
			soundStatus.enabled = false;
		}
		else
		{
			Debug.Log("Turn on Sound Effect");
			DataScript.SetGameState(GAMESTATE.HAS_SOUND_EFFECTS_ON);
			NGUITools.SetActive(soundStatus.gameObject, true);
			soundStatus.enabled = true;
		}
	}
	
	public void MusicOnClick()
	{
		if(DataScript.IsGameState(GAMESTATE.HAS_MUSIC_ON) != 0)
		{
			DataScript.TurnOffGameState(GAMESTATE.HAS_MUSIC_ON);
			musicStatus.enabled = false;
		}
		else
		{
			DataScript.SetGameState(GAMESTATE.HAS_MUSIC_ON);
			musicStatus.enabled = true;
		}
	}

	void disconnect()
	{
		//Debug.Log(Network.connections.Length);
		//if (Network.connections.Length > 0) {
			Network.Disconnect();
			Application.LoadLevel("LobbyScene");
		//}
	}
	
	public void QuitNow()
	{
		disconnect();
	}

	void OnPlayerDisconnected(NetworkPlayer player)
	{
		connectionTerminated.SetActive(true);
	}

	void Update()
	{
		/*
		if(!isDisconnectedWithPlayer)
		{
			if(Network.connections.Length < 2)
			{
				isDisconnectedWithPlayer = true;
				connectionTerminated.SetActive(true);
				//object[] allObjects = FindObjectsOfTypeAll(typeof(MonoBehaviour)) ;

				//Application.LoadLevel("logoScene");
			}
		}

		if(Input.GetKeyDown(KeyCode.Space))
		{
			connectionTerminated.SetActive(true);
		}*/
	}
	void OnDisconnectedFromServer()
	{
		connectionTerminated.SetActive(true);
	}

}
